package com.upcode.weighing.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author : liqi
 * Date: 2018-01-29
 * Time: 17:30
 */
public class DateUtils {
    public static SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static String PATTERND_TIME = "yyyy-MM-dd HH:mm:ss";
    public static String PATTERN_DATE = "yyyy-MM-dd";

    /**
     * @param unNormalDatetime
     * added by LeiDong to deal with the date from Web client,  such as : 2021-6-6
     * @return formatStr such as : 2021-06-06
     * */
    public static String transUnNormalDatetime(String unNormalDatetime){
        // 处理前端传过来的时间格式
        String [] delStr = unNormalDatetime.split("-");
        String year; String month; String day;
        year = delStr[0]; month = delStr[1]; day = delStr[2];
        if (delStr[1].length() < 2){
            month = "0" + delStr[1];
        }
        if (delStr[2].length() < 2){
            day = "0" + delStr[2];
        }
        return year + "-" + month + "-" + day + " 00:00:00";
    }

    /**
     * 将字符串转date
     * @param str
     * @param pattern "yyyy-MM-dd HH:mm:ss" "yyyy-MM-dd" "yyyy-MM-dd HH:mm"
     * @return
     */
    public static Date stringToDate(String str , String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            if(StringUtils.isNotEmpty(str)&&StringUtils.isNotEmpty(pattern)){
                return sdf.parse(str);
            }else{
                return null;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String dateToString(Date date,String format){
        if(date!=null){
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }else{
            return "";
        }
    }
    public static String dateToString(java.time.LocalDateTime date,String format){
        if(date!=null){
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
            String strDate2 = dtf.format(date);
            return strDate2;
        }else{
            return "";
        }
    }
    /**
     * long类型转为String
     * @return 格式yyyy-MM-dd ，精确到日
     */
    public static Date longToDate(long l) {
        try {
            Date dt = new Date(l);
            return dt;
        } catch (Exception e) {
            return null;
        }
    }

    public static String nowDateStr(String format){
        LocalDate date=LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        String strDate2 = dtf.format(date);
        return strDate2;
    }
    public static String nowDateStr(){
        LocalDate date=LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String strDate2 = dtf.format(date);
        return strDate2;
    }

    public static void main(String[] args){
//        String str="新疆6501";
//        try {
//            byte[] b=str.getBytes("GBK");
//            System.out.println(b.length);
//            System.out.println(bytesToHexString(b));
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        String time="2019-04-23T11:24:18";
//        DateTime dateTime2=DateTime.parse(time);
//        String dateTime=new LocalDateTime(dateTime2).toString("yyyyMMddHHmmss");
//        System.out.println(dateTime);
//        System.out.println(nowDateStr());
        String s = convertSecondsToHMS(37809);
        System.out.println(s);
    }

    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2){
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    public static String convertSecondsToHMS(long seconds) {
        long hours = TimeUnit.SECONDS.toHours(seconds);
        long remainingSeconds = seconds - TimeUnit.HOURS.toSeconds(hours);
        long minutes = TimeUnit.SECONDS.toMinutes(remainingSeconds);
        remainingSeconds = remainingSeconds - TimeUnit.MINUTES.toSeconds(minutes);

        return String.format("%d小时%d分%d秒", hours, minutes, remainingSeconds);
    }


    public static long getUTCTime(){
        Calendar cal = Calendar.getInstance();
        //获得时区和 GMT-0 的时间差,偏移量
        int offset = cal.get(Calendar.ZONE_OFFSET);
        //获得夏令时  时差
        int dstoff = cal.get(Calendar.DST_OFFSET);
        cal.add(Calendar.MILLISECOND, - (offset + dstoff));
        return cal.getTime().getTime();
    }

}
